package api

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"mogutouERP/models"
	"mogutouERP/utils/response"
)

// CreateComponent 创建新配件类型
func CreateComponent(c *gin.Context) {
	var data struct {
		ID                     string  `json:"id" binding:"required"`
		No                     string  `json:"no" binding:"required"`
		Name                   string  `json:"name" binding:"required"`
		MerchandiseSafetyStock uint    `json:"merchandiseSafetyStock"`
		PackgeNum              uint    `json:"packgeNum" `
		Weight                 float32 `json:"weight" `
		PackgeWeight           float32 `json:"packgeWeight"`
		PackgeCube             string  `json:"packgeCube"`
		Remark                 string  `json:"remark"`
		Supply                 string  `json:"supply" binding:"required"`
		Location               string  `json:"location" binding:"required"`
		PurchasePrice          float32 `json:"purchasePrice"`
	}
	err := c.ShouldBindJSON(&data)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: err.Error()})
		return
	}

	component := models.Component{
		ID:                     data.ID,
		No:                     data.No,
		Name:                   data.Name,
		MerchandiseSafetyStock: data.MerchandiseSafetyStock,
		PackgeNum:              data.PackgeNum,
		Weight:                 data.Weight,
		PackgeWeight:           data.PackgeWeight,
		PackgeCube:             data.PackgeCube,
		Remark:                 data.Remark,
		Supply:                 data.Supply,
		Location:               data.Location,
		PurchasePrice:          data.PurchasePrice,
	}
	err = models.CreateComponent(&component)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusCreated, models.GetComponent(data.ID))
}

// UpdateComponent 更新配件信息
func UpdateComponent(c *gin.Context) {
	componentID := c.Param("id")

	if !models.HaveComponent(componentID) {
		c.AbortWithStatusJSON(http.StatusNotFound, response.Error{Error: "没有此配件信息"})
		return
	}

	var data struct {
		No                     string  `json:"no"`
		Name                   string  `json:"name"`
		MerchandiseSafetyStock uint    `json:"merchandiseSafetyStock"`
		PackgeNum              uint    `json:"packgeNum"`
		Weight                 float32 `json:"weight"`
		PackgeCube             string  `json:"packgeCube"`
		PackgeWeight           float32 `json:"packgeWeight"`
		Supply                 string  `json:"supply" binding:"required"`
		Remark                 string  `json:"remark"`
		Location               string  `json:"location"`
		PurchasePrice          float32 `json:"purchasePrice"`
	}
	err := c.ShouldBindJSON(&data)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, response.Error{Error: err.Error()})
		return
	}

	component := models.Component{
		Name:                   data.Name,
		No:                     data.No,
		MerchandiseSafetyStock: data.MerchandiseSafetyStock,
		PackgeNum:              data.PackgeNum,
		Weight:                 data.Weight,
		PackgeWeight:           data.PackgeWeight,
		PackgeCube:             data.PackgeCube,
		Supply:                 data.Supply,
		Remark:                 data.Remark,
		Location:               data.Location,
		PurchasePrice:          data.PurchasePrice,
	}
	err = models.UpdateComponent(componentID, &component)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusCreated, gin.H{})
}

// DeleteComponent 删除配件
func DeleteComponent(c *gin.Context) {
	componentID := c.Param("id")

	if !models.HaveComponent(componentID) {
		c.AbortWithStatusJSON(http.StatusNotFound, response.Error{Error: "没有此配件信息"})
		return
	}

	err := models.DeleteComponent(componentID)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusNoContent, gin.H{})
}

// ListComponents 列举所有配件
func ListComponents(c *gin.Context) {
	components, err := models.GetComponents()
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusOK, components)

} // ListComponentsStock 列举所有配件，包括库存警告
func ListComponentsStock(c *gin.Context) {
	components, err := models.GetComponentsStock()
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusOK, components)
}

// ListComponentsAsAdmin list所有配件信息
func ListComponentsAsAdmin(c *gin.Context) {
	components, err := models.GetComponentsWithPurchasePrice()
	if err != nil {
		c.AbortWithStatusJSON(http.StatusInternalServerError, response.Error{Error: err.Error()})
		return
	}

	c.JSON(http.StatusOK, components)
}
